package nobu.nakata.nfcfare.resource.mapper.strategy;

import net.sf.jsr107cache.Cache;
import nobu.nakata.nfcfare.resource.item.ITrainResourceItem;
import nobu.nakata.nfcfare.resource.mapper.IPersistenceStrategy;

public class TrainCaching implements IPersistenceStrategy<ITrainResourceItem, Long> {
	private Caching<ITrainResourceItem, Long> caching;
	
	public TrainCaching(Cache cache) {
		this.caching		= new Caching<ITrainResourceItem, Long>(cache, new KeyFactory());
	}

	@Override
	public ITrainResourceItem get(Long key) {
		return caching.get(key);
	}
	@Override
	public void set(ITrainResourceItem item) {
		caching.set(item);
	}
	@Override
	public void remove(ITrainResourceItem item) {
		caching.remove(item);
	}
	
	private class KeyFactory implements ICacheKeyFactory<ITrainResourceItem, Long> {
		private final String KIND = ITrainResourceItem.class.getSimpleName();

		@Override
		public String makeCacheKeyFromValue(ITrainResourceItem item) {
			return makeCacheKey(item.getTrainCode());
		}

		@Override
		public String makeCacheKey(Long key) {
			return KIND + ":" + key;
		}
	}
}
